﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPIDesktopClient.Logic;
using PPIDesktopClient.General;
using PPIDesktopClient.Events;

namespace PPIDesktopClient
{
    public partial class frmUploadStats : frmDialogBase
    {
        protected int m_AmountAssetServer;
        protected int m_AmountPoleServer;
        protected int m_AmountImageServer;
        protected UploadProcess m_ProcessUploadData;
        protected UploadProcessImages  m_ProcessUploadImages;
        protected string m_OpenedDatabaseConnectionString = null;

        public string OpenedDatabaseConnectionString
        {
            get { return m_OpenedDatabaseConnectionString; }
            set { m_OpenedDatabaseConnectionString = value; }
        }


        public frmUploadStats()
        {
            m_ProcessUploadData = new UploadProcess();
            m_ProcessUploadData.UploaderProgressChanged += new EventHandler<ProgressChangedEventArgs>(m_ProcessUploadData_UploaderProgressChanged);
            m_ProcessUploadData.UploaderCompleted += new EventHandler(m_ProcessUploadData_UploaderCompleted);
            m_ProcessUploadData.UploadCancelled += new EventHandler(m_ProcessUploadData_UploadCancelled);
            m_ProcessUploadData.UploadingNewDatabase += new EventHandler(m_ProcessUploadData_UploadingNewDatabase);
            m_ProcessUploadData.UploadedDatabase += new EventHandler<PPIDesktopClient.Events.CompletedDatabaseArgs>(m_ProcessUploadData_UploadedDatabase);
            m_ProcessUploadData.UploadCancelledByInformation += new EventHandler<MessageArgs>(m_ProcessUploadData_UploadCancelledByInformation);

            m_ProcessUploadImages = new UploadProcessImages();
            m_ProcessUploadImages.UploaderProgressChanged += new EventHandler<ProgressChangedEventArgs>(m_ProcessUploadImages_UploaderProgressChanged);
            m_ProcessUploadImages.UploaderCompleted += new EventHandler(m_ProcessUploadImages_UploaderCompleted);
            m_ProcessUploadImages.UploadCancelled += new EventHandler(m_ProcessUploadImages_UploadCancelled);
            m_ProcessUploadImages.UploadingNewDatabase += new EventHandler(m_ProcessUploadImages_UploadingNewDatabase);
            InitializeComponent();
        }


        private void frmUploadStats_Load(object sender, EventArgs e)
        {
            if (!m_ProcessUploadImages.HasNextUpload() && !m_ProcessUploadData.HasNextUpload())
            {
                ctrlOutbox.LoadData(false, m_OpenedDatabaseConnectionString);
                this.Visible = true;
                RefreshShowValues();
                CheckHastNextProcessUpload();            
            }

        }

        private void CheckHastNextProcessUpload()
        {
            if (!ctrlOutbox.HasDatabase())
            {
                this.btnStart.Enabled = false;
                MessageBox.Show("Upload is up to date. No pending folders.");
            }
            else
            {
                this.btnStart.Enabled = true;
            }
        }

        private void RefreshShowValues()
        {
            //m_ProcessUploadImages.RefreshValues();
            //m_ProcessUploadData.RefreshValues();
            if (m_ProcessUploadData.HasNextUpload())
            {
                lblDatabaseName.Text = m_ProcessUploadData.DatabaseName;
                lblnuAssets.Text = m_ProcessUploadData.TotalAssets.ToString();
            }
            else
            {
                pbDataDownload.Value = 0;
                lblDatabaseName.Text = "";
                lblnuAssets.Text = "0";
            }
            if (m_ProcessUploadImages.HasNextUpload())
            {
                lblImageDatabase.Text = m_ProcessUploadImages.DatabaseName;
                lblnuImages.Text = m_ProcessUploadImages.TotalImages.ToString();
            }
            else
            {
                pbImageUpload.Value = 0;
                lblImageDatabase.Text = "";
                lblnuImages.Text = "0";
            }

            m_AmountAssetServer = LVendor_Visit_XMissionStructure.GetTotal();
            m_AmountImageServer = LVendor_Visit_XMissionStructures_Image.GetTotal();
            lblnuTotalAssetsServer.Text = m_AmountAssetServer.ToString();
            lblnuTotalImagesServer.Text = m_AmountImageServer.ToString();

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                this.btnStart.Enabled = false;
                this.btnStop.Enabled = true;
                this.ctrlOutbox.Enabled = false;
                m_ProcessUploadData.DatabasesToUpload = ctrlOutbox.GetChekeckDatabases();
                m_ProcessUploadData.StartUpload();

            }
            catch (Exception ex)
            {
                
                throw;
            }
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        #region Process Events
        void m_ProcessUploadImages_UploaderProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbImageUpload.Value = e.ProgressPercentage;
        }
        void m_ProcessUploadImages_UploaderCompleted(object sender, EventArgs e)
        {
            try
            {
                if (!m_ProcessUploadImages.HasNextUpload() && !m_ProcessUploadData.HasNextUpload())
                {
                    btnStart.Enabled = true;
                    btnStop.Enabled = false;
                    ctrlOutbox.Enabled = true;
                    ctrlOutbox.LoadData(false);
                    CheckHastNextProcessUpload();
                }
            }
            catch (Exception ex)
            {
                
                throw;
            }

        }
        void m_ProcessUploadImages_UploadCancelled(object sender, EventArgs e)
        {
            this.pbImageUpload.Value = 0;
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
            RefreshShowValues();
        }
        void m_ProcessUploadImages_UploadingNewDatabase(object sender, EventArgs e)
        {
            try
            {
                pbImageUpload.Value = 0;
                RefreshShowValues();
            }
            catch (Exception ex)
            {
                
                throw;
            }

        }

        void m_ProcessUploadData_UploaderCompleted(object sender, EventArgs e)
        {
            if (!m_ProcessUploadImages.HasNextUpload() && !m_ProcessUploadData.HasNextUpload())
            {
                btnStart.Enabled = true;
                btnStop.Enabled = false;
                ctrlOutbox.Enabled = true;
                ctrlOutbox.LoadData(false);
                CheckHastNextProcessUpload();
            }
        }
        void m_ProcessUploadData_UploaderProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbDataDownload.Value = e.ProgressPercentage;
        }
        void m_ProcessUploadData_UploadCancelled(object sender, EventArgs e)
        {
            this.pbDataDownload.Value = 0;
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
            RefreshShowValues();
        }
        void m_ProcessUploadData_UploadingNewDatabase(object sender, EventArgs e)
        {
            try
            {
                pbDataDownload.Value = 0;
                RefreshShowValues();
            }
            catch (Exception ex)
            {
                
                throw;
            }

        }
        void m_ProcessUploadData_UploadedDatabase(object sender, CompletedDatabaseArgs e)
        {
            try
            {
                m_ProcessUploadImages.StartUpload(e.Database);
            }
            catch (Exception ex)
            {
                
                throw;
            }

            
        }
        void m_ProcessUploadData_UploadCancelledByInformation(object sender, MessageArgs e)
        {
            this.pbDataDownload.Value = 0;
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
            RefreshShowValues();
            m_ProcessUploadImages.StopProcess();
            MessageBox.Show(e.Message);
        }

        
        #endregion

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.btnStop.Enabled = false;
            this.btnStart.Enabled = true;
            m_ProcessUploadData.StopProcess();
            m_ProcessUploadImages.StopProcess();
            this.ctrlOutbox.Enabled = true;
        }
    }
}
